const { getUserInfor, delUser, setUser, addUser } = require('./sql')

const userApi = async (ctx) => {
    const url = ctx.request.url
    const id = +url?.substring(url?.lastIndexOf("/") + 1) || null
    switch (url) {
        case `/user/del/${id}`:
            await delUser(id)
            const del = await getUserInfor()
            return body = {
                data: del.results || null,
                code: del.code
            }
        case `/user/set/${id}`:
            await setUser(id, ctx.request.body)
            const set = await getUserInfor()
            return body = {
                data: set.results || null,
                code: set.code
            }
        case `/user/add`:
            await addUser(ctx.request.body)
            const add = await getUserInfor()
            return body = {
                data: add.results || null,
                code: add.code
            }
        default:
            break;
    }
}

module.exports = {userApi}